﻿using AngleSharp.Dom;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Model.Dtos.Role;
using WomanMotion.WebApi.Model.Dtos.Users;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Repository.IRepository;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Service.Service
{
    public class RoleService : BaseCRUDService<Role, RoleVM, UpdateRoleDTO, CreatedRoleDTO>, IRoleService
    {
        private readonly IRoleRepository _roleRepository;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IAuthoritysService _authoritysService;

        public RoleService(ILogger<RoleService> logger, IMapper mapper, IRoleRepository roleRepository, IHttpContextAccessor httpContext, IAuthoritysService authoritysService) : base(logger, mapper, roleRepository, httpContext)
        {
            this._roleRepository = roleRepository;
            this._httpContextAccessor = httpContext;
            _authoritysService = authoritysService;
        }

        /// <summary>
        ///添加角色，因为涉及到添加权限，所以使用事务(异步)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public override async Task<RoleVM> CreatedAsync(CreatedRoleDTO dto)
        {
            var userid = httpContext.HttpContext.User.Claims.Where(m => m.Type == "UserId").First().Value;
            var res = await _roleRepository.InsertAsync(dto, long.Parse(userid));
            return res;
        }

        /// <summary>
        ///更新角色，因为涉及到添加权限，所以使用事务(异步)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public override async Task<RoleVM> UpdateAsync(UpdateRoleDTO dto)
        {
            var userid = httpContext.HttpContext.User.Claims.Where(m => m.Type == "UserId").First().Value;
            var res = await _roleRepository.UpdateAsync(dto, long.Parse(userid));
            return res;
        }

        /// <summary>
        ///分页查询带导航单表数据(异步)
        /// </summary>
        /// <param name="pars">查询参数</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public override async Task<PageViewBase<List<RoleVM>>> GetPageNavListAsync(QueryParameterBase pars, params string[] ignoreProperyNameList)
        {
            var data = await _roleRepository.GetPageNavListAsync(pars, ignoreProperyNameList);
            var res = new PageViewBase<List<RoleVM>>();
            res.Total = data.Total;
            res.PageNum = pars.PageNum;
            res.PageSize = pars.PageSize;
            var vms = _mapper.Map<List<Role>, List<RoleVM>>(data.Data);
            var newvms = new List<RoleVM>();
            foreach (var v in vms)
            {
                var treelist = new List<string>();
                var newvm = new RoleVM();
                newvm = v;
                //查询角色所具备的权限
                var listauth = await _authoritysService.GetListAsync(m => m.Roleid == v.Id);
                //var listcons = listauth.Select(a => a.Controllerid).Distinct().ToList();
                var listacts = listauth.Select(a => a.Actionsid).Distinct().ToList();
                //foreach (var item in listcons)
                //{
                //    treelist.Add("controller," + item);
                //}
                foreach (var item in listacts)
                {
                    treelist.Add("action," + item);
                }
                newvm.authoritystree = treelist.ToArray();
                newvms.Add(newvm);
            }
            res.Data = newvms;
            return res;
        }
    }
}